package br.com.onlinebidding.facade;

import java.util.List;

import br.com.onlinebidding.dao.EdictDAO;
import br.com.onlinebidding.model.Edict;

public class EdictFacade {
	private EdictDAO edictDAO = new EdictDAO();

	public void insertEdict(Edict edict) {
		this.edictDAO.beginTransaction();
		this.edictDAO.save(edict);
		this.edictDAO.commitAndCloseTransaction();
	}

	public void updateEdict(Edict edict) {
		this.edictDAO.beginTransaction();
		Edict persistedEdict = this.edictDAO.find(edict.getId());
		persistedEdict.setName(edict.getName());
		persistedEdict.setIdProduct(edict.getIdProduct());
		persistedEdict.setDescription(edict.getDescription());
		persistedEdict.setDateTimeStart(edict.getDateTimeStart());
		persistedEdict.setDateTimeFinish(edict.getDateTimeFinish());
		persistedEdict.setAmount(edict.getAmount());
		this.edictDAO.update(persistedEdict);
		this.edictDAO.commitAndCloseTransaction();
	}

	public Edict findEdict(int edictId) {
		this.edictDAO.beginTransaction();
		Edict edict = this.edictDAO.find(edictId);
		this.edictDAO.closeTransaction();
		return edict;
	}

	public List<Edict> listAll() {
		this.edictDAO.beginTransaction();
		List<Edict> result = this.edictDAO.findAll();
		this.edictDAO.closeTransaction();
		return result;
	}


	public List<Edict> listAllAssets() {
		this.edictDAO.beginTransaction();
		List<Edict> result = this.edictDAO.findAllAssets();
		this.edictDAO.closeTransaction();
		return result;
	}
	
	public void deleteEdict(Edict edict) {
		this.edictDAO.beginTransaction();
		Edict persistedEdict = this.edictDAO.findReferenceOnly(edict.getId());
		this.edictDAO.delete(persistedEdict);
		this.edictDAO.commitAndCloseTransaction();
	}

}